package com.sen.rbac.controller;

import cn.hutool.core.util.StrUtil;
import com.sen.common.common.controller.BaseController;
import com.sen.common.common.response.MessageResult;
import com.sen.common.common.util.CheckUtils;
import com.sen.rbac.constant.RbacConstant;
import com.sen.rbac.dto.SysRoleDto;
import com.sen.rbac.feign.RbacRoleFeign;
import com.sen.rbac.service.RoleService;
import com.sen.rbac.vo.SysRoleGroupVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.sen.rbac.enums.RbacMsgEnum.role_related_resource_failed;

/**
 * 角色服务
 * @author esjiang
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController implements RbacRoleFeign {

    @Resource
    RoleService roleService ;


    /**
     * 新增
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/add")
    public MessageResult add(@RequestBody SysRoleDto sysRoleDto){
        return crud(sysRoleDto);
    }

    /**
     * 修改
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/update")
    public MessageResult update(@RequestBody SysRoleDto sysRoleDto){
        return crud(sysRoleDto);
    }

    /**
     * 删除
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/del")
    public MessageResult delete(@RequestBody SysRoleDto sysRoleDto){
        return crud(sysRoleDto);
    }

    /**
     * 查询
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/list")
    public MessageResult list(@RequestBody SysRoleDto sysRoleDto){
        return crud(sysRoleDto);
    }

    /**
     * 增删改查
     * @param sysRoleDto
     * @return
     */
    public MessageResult crud(@RequestBody SysRoleDto sysRoleDto){
        return exec("CRUD角色",sysRoleDto,()->{
            CheckUtils.checkNotNull(sysRoleDto,"operationType");
            String operationType = sysRoleDto.getOperationType();
            if (StrUtil.equals(operationType, RbacConstant.ADD) ){
                CheckUtils.checkNotNull(sysRoleDto,"name");
            }
            if (StrUtil.equals(operationType,RbacConstant.UPD)){
                CheckUtils.checkNotNull(sysRoleDto,"code","name");
            }
            if (StrUtil.equals(operationType, RbacConstant.DEL)){
                CheckUtils.checkNotNull(sysRoleDto,"code");
            }
            return roleService.crud(sysRoleDto);
        });
    }


    /**
     * 查询角色组
     * @return
     */
    @GetMapping("/roleGroup")
    public MessageResult getRoleGroup(){
        return exec("查询角色组",null,()->{
            List<SysRoleGroupVO> roleGroup = roleService.findRoleGroup();
            return MessageResult.success(roleGroup);
        });
    }

    /**
     * 关联资源
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/relatedResource")
    public MessageResult relatedResources(@RequestBody SysRoleDto sysRoleDto){
        return exec("关联资源",sysRoleDto,()->{
            CheckUtils.checkNotNull(sysRoleDto,"code","resources");
            if (roleService.relatedResource(sysRoleDto.getCode(),sysRoleDto.getResources())){
                return MessageResult.success(null) ;
            }
            return MessageResult.bizFailed(role_related_resource_failed);
        });
    }

}
